<?php

/**
 * This is the model class for table "ld_withdraw".
 *
 * The followings are the available columns in table 'ld_withdraw':
 * @property integer $serialno
 * @property string $request_withdraw_date
 * @property double $money
 * @property string $currency
 * @property integer $status
 * @property integer $user_id
 * @property string $send_date
 * @property integer $send_by
 */
class Withdraw extends BaseActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Withdraw the static model class
	 */
	
	public $user_name;
	public $lr_account;
	public static function model($className=__CLASS__)
	{
		
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'ld_withdraw';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('serialno, status, user_id, send_by', 'numerical', 'integerOnly'=>true),
			array('money', 'numerical'),
			array('currency', 'length', 'max'=>255),
			array('request_withdraw_date, send_date', 'safe'),
			array('user_name','length', 'max'=>255),
				array('lr_account','length', 'max'=>15),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('serialno, request_withdraw_date, money, currency, status, user_id, send_date, send_by', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'serialno' => 'ID',
			'user_name' =>'Người yêu cầu chuyển',
			'request_withdraw_date' => 'Ngày yêu cầu chuyển tiền',
			'money' => 'Số tiền',
			'currency' => 'Loại tiền',
			'status' => 'Trạng thái',
			'send_date' => 'Ngày chuyển tiền',
			'send_by' => 'Người chuyển',
		);
	}

        public  static function getWithdrawHistoryByUserId($user_id)
        {
            $query = "SELECT a.serialno,a.request_withdraw_date as real_date, DATE_FORMAT(a.request_withdraw_date,  '%Y-%m-%d %H:%i:%s') as request_withdraw_date, a.money, a.currency, 
                        IF(a.status = 0,'Đang chờ','Đã chuyển') as status, DATE_FORMAT(a.send_date, '%Y-%m-%d %H:%i:%s') as send_date, 
                        c.user_name as send_by,a.batch as batch
                        FROM ld_withdraw a INNER JOIN ld_user b on a.user_id = b.user_id LEFT JOIN ld_user c ON a.send_by = c.user_id
                        WHERE a.user_id = $user_id
                        ORDER BY real_date DESC, status ASC";
            return Withdraw::model()->findAllBySql($query);
        }
        
        public  static function getWithdrawNotDone()
        {
        	$query = "SELECT a.serialno, b.user_name as user_name, b.lr_acc as lr_account,a.request_withdraw_date as real_date,
        	DATE_FORMAT(a.request_withdraw_date, '%H:%i:%s %d-%m-%Y') as request_withdraw_date,
        	a.money,
        	a.currency,
        	DATE_FORMAT(a.send_date, '%H:%s:%i %d-%m-%Y') as send_date,
        	c.user_name as send_by
        	FROM ld_withdraw a INNER JOIN ld_user b on a.user_id = b.user_id LEFT JOIN ld_user c ON a.send_by = c.user_id
        	WHERE a.status=0
        	ORDER BY real_date DESC, status ASC";
        	return Withdraw::model()->findAllBySql($query);
        }        
        
        public  static function getWithdrawHistoryAll()
        {
        	$query = "SELECT a.serialno, b.user_name as user_name, b.lr_acc as lr_account,a.request_withdraw_date as real_date, 
        	DATE_FORMAT(a.request_withdraw_date, '%H:%i:%s %d-%m-%Y') as request_withdraw_date, 
        	a.money, 
        	a.currency,
        	DATE_FORMAT(a.send_date, '%H:%s:%i %d-%m-%Y') as send_date,
        	c.user_name as send_by
        	FROM ld_withdraw a INNER JOIN ld_user b on a.user_id = b.user_id LEFT JOIN ld_user c ON a.send_by = c.user_id
        	
        	ORDER BY real_date DESC";
        	return Withdraw::model()->findAllBySql($query);
        }
}